All video tapes will have a rental price,
so the fixes should be made to the parent class VideoTape
.
A new variable rent
should be added to the parent class.
Then modify its constructor and its show()
method.
The two child classes will inherit these changes.
Fixing the parent class fixes all of its children.
Object
Class
Remember the rule: every constructor starts out
with a super()
constructor.
If you don't explicitly put it in,
then the Java compiler automatically puts it in for you.
Now look at the definition of VideoTape
:
class VideoTape { String title; // name of the item int length; // number of minutes boolean avail; // is the tape in the store? // constructor public VideoTape( String ttl, int lngth ) { title = ttl; length = lngth; avail = true; } public void show() { System.out.println( title + ", " + length + " min. available:" + avail ); } }
According to the rule, the compiler automatically does this:
// constructor
public VideoTape( String ttl, int lngth )
{
super(); // use the super class's constuctor
title = ttl; length = lngth; avail = true;
}
This is correct.
All classes have a parent class (a super class) except one.
The class at the top of the Java class hierarchy is called
Object
.
If a class definition does not specify a parent class then
it automatically has Object
as a parent class.
The compiler automatically assumes, for example:
class VideoTape extends Object
{
. . .
}